#!/usr/bin/env python
# encoding: utf-8
'''
@author: Excelsiorly
@license: (C) Copyright 2022, All Rights Reserved.
@contact: excelsiorly@qq.com
@file: 110. 平衡二叉树.py
@time: 2022/1/18 17:24
@desc: https://leetcode-cn.com/problems/balanced-binary-tree/
> 给定一个二叉树，判断它是否是高度平衡的二叉树。
本题中，一棵高度平衡二叉树定义为：
    一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。

1. Ot(n2), Os(n)
'''
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root: return True
        def height(root):
            if not root: return 0
            return max(height(root.left), height(root.right))+1
        return abs(height(root.left) - height(root.right))<=1 and self.isBalanced(root.left) and self.isBalanced(root.right)